package com.bestcem.xm.survey.util;

import com.bestcem.xm.common.core.constant.ReqErrorCodeConstants;
import com.bestcem.xm.common.core.exception.BusinessException;
import lombok.experimental.UtilityClass;
import lombok.extern.slf4j.Slf4j;

import java.util.List;

import static com.bestcem.xm.survey.util.SurveyConstant.FULL_TYPE_LIST;
import static com.bestcem.xm.survey.util.SurveyConstant.TYPE_LIMIT_LIST;

@Slf4j
@UtilityClass
public class CommonUtil {
    /**
     * 检查上传文件后缀名
     *
     * @param fileName  上传文件名 xxx.txt
     * @param typeLimit 文件类型: upload_enums.TYPE_LIMIT_LIST
     */
    public void checkExt(String fileName, String typeLimit) {
        fileName = fileName.substring(fileName.lastIndexOf('.') + 1);
        List<String> extList = TYPE_LIMIT_LIST.getOrDefault(typeLimit, FULL_TYPE_LIST);
        if (!extList.contains(fileName)) {
            throw new BusinessException(String.valueOf(ReqErrorCodeConstants.FILE_TYPE_ERROR), "filetype not allowed");
        }
    }
}
